<template>
  <div class="c-pv20 c-ph24">
    <div class="c-mb20" v-if="isTest">
      <input type="text" v-model="userId" placeholder="请输入用户的uerId" class="c-hh60 c-fs20 c-ph20" />
      <button class="c-bg-mb1-orange c-fc-white c-fs20 c-pv8 c-ph8 c-ml12" @click="clickSeach">查询并进入首页</button>
      <div class="c-fs20 c-fc-red">注：进入首页后请刷新页面，不然页面会报错</div>
    </div>
    <button class="c-bg-mb1-orange c-fc-white c-fs32 c-pv20 c-ph20" @click="clickClear">点击清除缓存</button>
    <h1>缓存内容</h1>
    <div v-for="(item,index) in localStorageList" :key="index">

      <div class="c-wb-ba">缓存名称: {{item.name}}</div>
      <div class="c-fc-red c-wb-ba">缓存值: {{item.value}}</div>

    </div>
  </div>
</template>

<script>
import { utilJs } from "@/utils/common.js";
export default {
  name: "ClearLocalStorage",
  components: {
  },
  data() {
    return {
      localStorageList: [],
      userId: '',
      isTest: false, //是否是测试环境
    };
  },
  created() {},
  mounted() {
    this.isTest = global.apiurl.indexOf("kpapi-cs") != -1 ? true : false;
    this.$store.commit('updateLoadingStatus', {
      isLoading: false
    });
    this.getLocalStorage();
  },
  methods: {
    clickSeach() {
      utilJs.postMethod(global.apiurl + "mp/tymonTokenUtils/getTokenByTestHashedUserId/" + this.userId, {}, (res) => {
        let u = navigator.userAgent;
        let isAnd = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;//andrio终端
        window.localStorage.setItem("companyId", res.hc);
        window.localStorage.setItem(res.hc, res.hc);
        window.localStorage.setItem("userId", res.hu);
        window.localStorage.setItem("token", res.ht);
        window.localStorage.setItem(res.hc + "|token", res.ht);
        if (isAnd && window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger') { //测试用
          let exp = new Date();
          exp.setTime(exp.getTime() + 30 * 24 * 60 * 60 * 1000);
          document.cookie = res.hc + "|token=" + res.ht + ";expires=" + exp.toGMTString() + ";path=/";
        }
        this.$routerGo(this, "push", { path: '/homePage/about/companyProfile' });
      });
    },
    clickClear() {
      if ((navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Adr') > -1) && window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger' && localStorage.getItem("companyId")) {
        // 把之前的cookie设置过期，防止cookie不停地增加导致http request 400错误
        let exp = new Date();
        exp.setTime(exp.getTime() - 1);
        document.cookie = localStorage.getItem("companyId") + "|token=;expires=" + exp.toGMTString() + ";path=/";
      }
      window.localStorage.clear();
      alert("清除成功");
    },
    getLocalStorage() {
      for (let key in localStorage) {
        let item = {
          name: key,
          value: localStorage.getItem(key)
        };
        this.localStorageList.push(item);
      }
    }
  }
};
</script>

